﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using FreeBitExcel.entity;
using log4net;

namespace FreeBitExcel
{
    public class ExcelService
    {
        public ILog Logger => this.GetLogger();
        public DataSet DataSet;


        public IList<string> GetTablenames()
        {
            var tableList = new List<string>();
            if (DataSet?.Tables != null)
                tableList.AddRange(from object table in DataSet?.Tables select table.ToString());
            Logger.Info("读取列表成功,size = " + tableList.Count);
            tableList.Reverse();
            return tableList;
        }

        public async void startExce(string name)
        {
            var result = await reslovePersons(name);
        }

        private Task<List<Person>> reslovePersons(string name)
        {
            return Task.Run(() =>
            {
                List<Person> list = new List<Person>();
                var dataSetTable = DataSet.Tables[name];
                GetValues(name);
                return list;
            });
        }


        public void GetValues(string sheetName)
        {
            foreach (DataRow row in DataSet.Tables[sheetName].Rows)
            {
                var rowTable = row.Table;
                if (rowTable != null && rowTable.Rows.Count >0)
                {
                    Console.WriteLine(@">>>>{0} ", rowTable.Rows);
                }
                foreach (var value in row.ItemArray)
                {
                    if (value is DBNull)
                    {
                        break;
                    }
                    Console.WriteLine(@"{0} : {1}", value, value.GetType());
                }
            }
        }
    }
}